package  
{
	import adobe.utils.CustomActions;
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Jay Wagner
	 */
	public class MainMenuState extends FlxState 
	{
		[Embed(source = "../data/fonts/18thCentury.ttf", fontFamily = "18thCentury", embedAsCFF = "false")] public static const font1:String;
		[Embed(source = "../data/fonts/PowellAntique.ttf", fontFamily = "PowellAntique", embedAsCFF = "false")] public static const font2:String;
		[Embed(source = "../data/fonts/PowellAntique-Bold.ttf", fontFamily = "PowellAntiqueBold", embedAsCFF = "false")] public static const font3:String;
		[Embed(source = "../data/fonts/fighting spirit TBS.ttf", fontFamily = "FightingSpirit", embedAsCFF = "false")] public static const font4:String;
		
		//public var FPSDisplay:FrameRate;
		
		private var _playButton:FlxButtonPlus;
		private var _playText1:FlxText;
		private var _playText2:FlxText;
		private var _pButton1:FlxSprite;
		private var _pButton2:FlxSprite;
		
		private var _levelSelectButton:FlxButtonPlus;
		private var _levelSelectText1:FlxText;
		private var _levelSelectText2:FlxText;
		private var _lButton1:FlxSprite;
		private var _lButton2:FlxSprite;
		
		private var _extrasBackground:FlxSprite;
		private var _extrasButton:FlxButtonPlus;
		private var _extrasButtonText1:FlxText;
		private var _extrasButtonText2:FlxText;
		private var _eButton1:FlxSprite;
		private var _eButton2:FlxSprite;
		
		private var _fadeInTimer:Number = 0.0;
		private const _fadeInMaxTime:Number = 1.0;
		
		private var _fadeTimer:Number = 0.0;
		private var _fadeTimeMax:Number = 1.0;
		private var _buttonPressed:Boolean = false;
		private var _fromOmnistrain:Boolean = false;
		private var _signAtTop:Boolean = true;
		private var _startMusicOnLaunch:Boolean;
		private var _toWhere:uint;
		
		private var _fadeSprite:FlxSprite;
		private var _bgLight:FlxSprite;
		private var _menuSign:FlxSprite;
		private var _signLightsUpper:FlxSprite;
		private var _signLightsLower:FlxSprite;
		private var _nutsAndVolts:FlxSprite;
		
		private const TOSTART:uint = 0;
		private const TOLEVELSELECT:uint = 1;
		private const TOEXTRAS:uint = 2;
		
		private var _armorLinkButton:FlxButtonPlus;
		private var _armorLinkSprite:FlxSprite;
		private var _westwoodLinkButton:FlxButtonPlus;
		private var _westwoodLinkSprite:FlxSprite;
		
		public function MainMenuState(fromOmnistrain:Boolean = false, startMusicOnLaunch:Boolean = true)
		{
			_fromOmnistrain = fromOmnistrain;
			_startMusicOnLaunch = startMusicOnLaunch;
		}
		
		override public function create():void
		{
			const BUTTON_WIDTH:uint = 270;
			const BUTTON_HEIGHT:uint = 45;
			
			//FPSDisplay = new FrameRate(); 
			//add(FPSDisplay.fps);
			
			var background:FlxSprite = new FlxSprite(0, 0, ImageFiles.menuBackground);
			add(background);

			_signLightsUpper = new FlxSprite(180, 274, ImageFiles.menuLightsOff);
			add(_signLightsUpper);
			_signLightsLower = new FlxSprite(180, 346, ImageFiles.menuLightsOff);
			add(_signLightsLower);
			
			_menuSign = new FlxSprite(0, 0, ImageFiles.menuMainSign);
			add(_menuSign);
			
			_extrasBackground = new FlxSprite(0, FlxG.height, ImageFiles.menuLeftButton);
			_extrasBackground.y -= _extrasBackground.height;
			_extrasBackground.exists = false;
			add(_extrasBackground);
			
			if (_fromOmnistrain)
			{
				_bgLight = new FlxSprite(0, 0);
				_bgLight.createGraphic(FlxG.width, FlxG.height, 0xff424242);
			}
			else
			{
				_bgLight = new FlxSprite(0, 0, ImageFiles.menuBackgroundLight);
			}
			_bgLight.blend = "multiply";
			add(_bgLight);
			
			_extrasButton = new FlxButtonPlus(0, FlxG.height - 60, onExtras);
			_extrasButtonText1 = new FlxText(-30, 10, BUTTON_WIDTH, "Extras").setFormat("FightingSpirit", 30, 0x729954, "center");
			_extrasButtonText2 = new FlxText(_extrasButtonText1.x, _extrasButtonText1.y, _extrasButtonText1.width, _extrasButtonText1.text).setFormat("FightingSpirit", 36, 0xd8eba2, "center");
			_eButton1 = new FlxSprite().createGraphic(200, 60, 0x003a5c39);
			_eButton2 = new FlxSprite().createGraphic(200, 60, 0x00729954);
			_extrasButton.loadGraphic(_eButton1, _eButton2);
			_extrasButton.setMouseOverCallback(mouseOverSound);
			_extrasButton.loadText(_extrasButtonText1, _extrasButtonText2);
			_extrasButton.exists = false;
			
			_nutsAndVolts = new FlxSprite(0, 0, ImageFiles.nutsAndVolts);
			add(_nutsAndVolts);
			
			if (_fromOmnistrain)
			{
				_menuSign.y += 480 - 18;
				_menuSign.velocity.y = -500;
				_nutsAndVolts.y += 480 - 18;
				_nutsAndVolts.velocity.y = -500;
				_signLightsLower.y += 480 - 18;
				_signLightsLower.velocity.y = -500;
				_signLightsUpper.y += 480 - 18;
				_signLightsUpper.velocity.y = -500;
				
				_signAtTop = false;
				
				FlxG.play(SoundFiles.mainMenuSignUpSound);
			}
			
			_playButton = new FlxButtonPlus(176, 272, onStart);
			_playButton.setMouseOverCallback(mouseOverPlay);
			_playButton.setMouseOutCallback(mouseOutPlay);
			_playText1 = new FlxText(-4, 0, BUTTON_WIDTH, "Play").setFormat("FightingSpirit", 54, 0xaaaaff, "center");
			_playText2 = new FlxText(_playText1.x, _playText1.y, _playText1.width, _playText1.text).setFormat("FightingSpirit", 60, 0xccccff, "center");
			_pButton1 = new FlxSprite().createGraphic(BUTTON_WIDTH, BUTTON_HEIGHT + 14, 0x00000000);
			_pButton2 = new FlxSprite().createGraphic(BUTTON_WIDTH, BUTTON_HEIGHT + 14, 0x00000000);
			_playButton.loadGraphic(_pButton1, _pButton2);
			if (_fromOmnistrain)
			{
				_playButton.exists = false;
			}
			_playButton.loadText(_playText1, _playText2);
			
			_levelSelectButton = new FlxButtonPlus(176, 345, onLevelSelect);
			_levelSelectButton.setMouseOverCallback(mouseOverLevelSelect);
			_levelSelectButton.setMouseOutCallback(mouseOutLevelSelect);
			_levelSelectText1 = new FlxText(-2, 10, BUTTON_WIDTH, "Level Select").setFormat("FightingSpirit", 20, 0xaaaaff, "center");
			_levelSelectText2 = new FlxText(_levelSelectText1.x, _levelSelectText1.y, _levelSelectText1.width, _levelSelectText1.text).setFormat("FightingSpirit", 22, 0xccccff, "center");
			_lButton1 = new FlxSprite().createGraphic(BUTTON_WIDTH, BUTTON_HEIGHT, 0x00000000);
			_lButton2 = new FlxSprite().createGraphic(BUTTON_WIDTH, BUTTON_HEIGHT, 0x00000000);
			_levelSelectButton.loadGraphic(_lButton1, _lButton2);
			if (_fromOmnistrain)
			{
				_levelSelectButton.exists = false;
			}
			_levelSelectButton.loadText(_levelSelectText1, _levelSelectText2);
			
			//westwood button
			_westwoodLinkButton = new FlxButtonPlus(0, 0, goToWestwoodSite);
			_westwoodLinkButton.setMouseOverCallback(mouseOverSound);
			_westwoodLinkSprite = new FlxSprite(0, 0, ImageFiles.westwoodLogo);
			var _tempWestwoodLinkSprite:FlxSprite = new FlxSprite(0, 0, ImageFiles.westwoodLogoHighlight);
			_westwoodLinkButton.loadGraphic(_westwoodLinkSprite, _tempWestwoodLinkSprite);
			_westwoodLinkButton.x = FlxG.width - _westwoodLinkButton.width;
			_westwoodLinkButton.y = FlxG.height - _westwoodLinkButton.height;
			
			//armor games button
			_armorLinkButton = new FlxButtonPlus(0, 0, goToArmorGamesSite);
			_armorLinkButton.setMouseOverCallback(mouseOverSound);
			_armorLinkSprite = new FlxSprite(0, 0, ImageFiles.armorLogo);
			var _tempArmorLinkSprite:FlxSprite = new FlxSprite(0, 0, ImageFiles.armorLogoHighlight);
			_armorLinkButton.loadGraphic(_armorLinkSprite, _tempArmorLinkSprite);
			_armorLinkButton.y = FlxG.height - _armorLinkButton.height;
			_armorLinkButton.x = _westwoodLinkButton.x - _armorLinkButton.width - 1;
			
			if (_fromOmnistrain)
			{
				_westwoodLinkButton.exists = false;
				_armorLinkButton.exists = false;
			}
			
			add(_armorLinkButton);
			add(_westwoodLinkButton);
			
			add(_playButton);
			add(_levelSelectButton);
			add(_extrasButton);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 1;
			add(_fadeSprite);
			
			if (_startMusicOnLaunch)
			{
				FlxG.playMusic(SoundFiles.themeMusic);
			}
		}
		
		override public function update():void
		{
			super.update();
			
			if (_fadeTimer < _fadeInMaxTime && !_buttonPressed)
			{
				_fadeTimer += FlxG.elapsed;
				
				_fadeSprite.alpha = Math.abs(1 - (_fadeTimer / _fadeInMaxTime));
			}
			
			if (_buttonPressed)
			{
				fadeOut();
			}
			
			//Only appears after they have completed the game.
			if (FlxG.scores[4] > 0)
			{
				_extrasButton.exists = true;
				_extrasBackground.exists = true;
			}
			
			if (_menuSign.y < 0)
			{
				FlxG.play(SoundFiles.mainMenuSignSound);
				
				_menuSign.y = 0;
				_menuSign.velocity.y = 0;
				_nutsAndVolts.y = 0;
				_nutsAndVolts.velocity.y = 0;
				_signLightsUpper.y = 274;
				_signLightsUpper.velocity.y = 0;
				_signLightsLower.y = 346;
				_signLightsLower.velocity.y = 0;
				_signAtTop = true;
				_playButton.exists = true;
				_levelSelectButton.exists = true;
				_westwoodLinkButton.exists = true;
				_armorLinkButton.exists = true;
				_bgLight.loadGraphic(ImageFiles.menuBackgroundLight);
				
				FlxG.flash.start(0xffffffff, .5);
				FlxG.quake.start(0.01, 1.0);
			}
			
			FlxG.flash.update();
			FlxG.quake.update();
			
			//FPSDisplay.Update();
		}
		
		private function onStart():void
		{
			if (!FlxG.pause && !_buttonPressed && _fadeSprite.alpha < .80)
			{
				FlxG.play(SoundFiles.selectPlaySound, .75);
				_buttonPressed = true;
				_toWhere = TOSTART;
				FlxG.level = 0;
				//if (fadeOut())
				//{
					//FlxG.state = new OpeningCinematicState();
				//}
			}
		}
		
		private function onLevelSelect():void
		{
			if (!FlxG.pause && !_buttonPressed && _fadeSprite.alpha < .80)
			{
				FlxG.play(SoundFiles.selectSound, 0.75);
				_buttonPressed = true;
				_toWhere = TOLEVELSELECT;
				//if (fadeOut())
				//{
					//FlxG.state = new LevelSelectState();
				//}
			}
		}
		
		private function onExtras():void
		{
			if (!FlxG.pause && !_buttonPressed && _fadeSprite.alpha < .80)
			{
				FlxG.play(SoundFiles.selectSound, .75);
				_buttonPressed = true;
				_toWhere = TOEXTRAS;
				//if (fadeOut())
				//{
					//FlxG.state = new CreditsState();
				//}
			}
		}
		
		private function mouseOverPlay():void
		{
			if (_signAtTop)
			{
				mouseOverSound();
				_signLightsUpper.loadGraphic(ImageFiles.menuLightsOn);
			}
		}
		
		private function mouseOutPlay():void
		{
			if (_signAtTop)
			{
				_signLightsUpper.loadGraphic(ImageFiles.menuLightsOff);
			}
		}
		
		private function mouseOverLevelSelect():void
		{
			if (_signAtTop)
			{
				mouseOverSound();
				_signLightsLower.loadGraphic(ImageFiles.menuLightsOn);
			}
		}
		
		private function mouseOutLevelSelect():void
		{
			if (_signAtTop)
			{
				_signLightsLower.loadGraphic(ImageFiles.menuLightsOff);
			}
		}
		
		private function mouseOverSound():void
		{
			if (_signAtTop)
			{
				FlxG.play(SoundFiles.mouseOverSound);
			}
		}
		
		private function fadeOut():void
		{
			_fadeTimer = _fadeSprite.alpha / _fadeTimeMax;
			_fadeTimer += FlxG.elapsed;
			
			if (_fadeTimer / _fadeTimeMax > _fadeSprite.alpha)
			{
				_fadeSprite.alpha = _fadeTimer / _fadeTimeMax;
			}
			
			if (_fadeTimer >= _fadeTimeMax)
			{
				switch (_toWhere)
				{
					case TOSTART:
					FlxG.state = new OpeningCinematicState;
					break;
					case TOLEVELSELECT:
					FlxG.state = new LevelSelectState(true);
					break;
					case TOEXTRAS:
					FlxG.state = new ExtrasState(true);
					break;
				}
			}
		}
		
		private function goToArmorGamesSite():void
		{
			if (!FlxG.pause && _fadeSprite.alpha < .80)
			{
				FlxU.openURL("http://armorgames.com/");
			}
		}
		
		private function goToWestwoodSite():void
		{
			if (!FlxG.pause && _fadeSprite.alpha < .80)
			{
				FlxU.openURL("http://www.westwood.edu/");
			}
		}
	}
}